<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.userName" placeholder="学员姓名" clearable  class="w165"></el-input>
                    <el-input v-model="search.exerciseTeacher" placeholder="教练姓名" clearable  class="w165"></el-input>
                    <el-input v-model="search.agentName" placeholder="机构" clearable  class="w165"></el-input>
                    <el-select v-model="search.courseType" placeholder="课程类型" clearable filterable >
                        <el-option label="30分钟课" :value="1"></el-option>
                        <el-option label="60分钟课" :value="2"></el-option>
                        <el-option label="体验课" :value="4"></el-option>
					</el-select>
                    <el-date-picker v-model="search.timeRange" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss"
							range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" class="sDateSelect" style="margin-left:10px;"
						>
                    </el-date-picker>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-download" @click="exportHander">导出</el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.articleExercise.list" :params="search" remoteSort @dataChange="dataChange" :exportData="exportData" :formatExportData="formatExportData">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="学员" prop="user" align="center" width="120">
                        <template #default="scope">
                            {{scope.row.user ? scope.row.user.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="机构" prop="agent" align="center" width="160">
                        <template #default="scope">
                            {{scope.row.agent ? scope.row.agent.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="教练" prop="exerciseTeacher" align="center" width="140">
                        <template #default="scope">
                            {{scope.row.exerciseTeacher ? scope.row.exerciseTeacher.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="课程名称" prop="course" min-width="120" align="center" show-overflow-tooltip>
                        <template #default="scope">
                            {{scope.row.course ? scope.row.course.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="课程类型" prop="words" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.courseType ? scope.row.courseType.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="交付方式" prop="listenType" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.listenType ? scope.row.listenType.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="上课时间" width="150" align="center">
                        <template #default="scope">
                            <span>{{ scope.row.startTime }}</span><br>
                            <span>{{ scope.row.endTime }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="150">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>


    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>

</template>

<script>
import detailDialog from './detail'
export default {
    components: {
        detailDialog
    },
    data() {
        return {
            dialog: {
                detail: false
            },
            search: {
                name: null,
                learnType: 1,
                userId: null
            },
            exportData: {
                header: ['机构名称', '课程名称', '学员姓名', '学员手机号', '教练账号', '教练姓名', '课程类型', '交付方式', '上课时间', '结束时间'],
                column: ['agent', 'courseName', 'userName', 'userMobile', 'teacherAccount', 'exerciseTeacher', 'courseType', 'listenType', 'startTime', 'endTime'],
                filename: '阅读理解上课列表',
                formatData: true
            },
            currentExercise: null
        }
    },
    mounted() {
        if (this.user) {
            this.search.userId = this.user.id
        }
    },
    methods: {
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = page * perPage + index - perPage + 1
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
            this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id)
			})
        },
        exportHander() {
			this.$refs.table.exportHander(this.search)
		},
		formatExportData(item) {
            item.agent = item.agent ? item.agent.name : ''
            item.courseName = item.course ? item.course.name : ''
            item.userName = item.user ? item.user.name : ''
            item.userMobile = item.user ? item.user.mobile : ''
            item.teacherAccount = item.exerciseTeacher ? item.exerciseTeacher.account : ''
            item.exerciseTeacher = item.exerciseTeacher ? item.exerciseTeacher.name : ''
            item.courseType = item.courseType ? item.courseType.name : ''
            item.listenType = item.listenType ? item.listenType.name : ''
            return item
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
.position {
    display: flex;
    align-items: center;
}
.position-item {
    margin-right: 5px;
}
.position-item:last-child {
    margin-right: 0;
}
</style>
